# -*- coding: utf-8 -*-
"""
@author:xuyuntao
@time:2021/5/11:9:36
@email:xuyuntao@189.cn
"""
from charactersAlgorithm.highCum import C20,C21,C40,C41,C42,\
    C60,C61,C62,C63,M20,M21,M40,M41,M42,M60,M61,M62,M63,\
    highCumMethods,f_x1,f_x2,f_x3,f_x1_avg,f_x2_avg,f_x3_avg,\
    C20_avg,C21_avg,C40_avg,C41_avg,C42_avg,\
    C60_avg,C61_avg,C62_avg,C63_avg
from charactersAlgorithm.instInf import R,A_cn_m,Gamma_max,\
    Sigma_aa,Phi_NL_m,Sigma_ap,F_NL_m,Sigma_af,\
    R_avg,Gamma_max_avg,Sigma_aa_avg,Sigma_ap_avg,Sigma_af_avg
import numpy
import collections

characters_no_parameter=\
    collections.namedtuple("characters_no_parameter",("r","gamma_max",
                                                      "sigma_aa","F_x1",
                                                      "F_x2","F_x3"))

characters_parameter=collections.namedtuple("characters_parameter",
                                            ("r","gamma_max",
                                             "sigma_aa","F_x1","F_x2","F_x3",
                                             "sigma_ap","sigma_af"))

charaReco=collections.namedtuple("charaReco",("f_x1","f_x2","f_x3","C20","C21","C63","sigma_af"))

def getCharacters(constel:numpy.ndarray,signal:numpy.ndarray,timeSeries=None,carrierFreq:int=None,a_t:float=1.0,noiseRad:float=0.1*numpy.pi):
    """通过一个函数获取一个信号的所有特征值，
    当timeSeries,carrierFreq没有传入参数时返回characters_no_parameter结构数据，
    即没有包含Sigma_ap,Sigma_af的结构数据，否则返回"""
    r=R(signal)
    # a_cn=A_cn(signal)
    gamma_max=Gamma_max(signal)
    sigma_aa=Sigma_aa(signal)
    F_x1 = f_x1(constel)
    F_x2 = f_x2(constel)
    F_x3 = f_x3(constel)
    if type(timeSeries)==numpy.ndarray:
        if carrierFreq==None:
            pass
        else:
            sigma_ap = Sigma_ap(signal,timeSeries,carrierFreq,a_t,noiseRad)
            sigma_af = Sigma_af(signal, timeSeries, carrierFreq, a_t)
            return characters_parameter(r,gamma_max,sigma_aa,F_x1,F_x2,F_x3,sigma_ap,sigma_af)
    else:
        pass
    return characters_no_parameter(r, gamma_max, sigma_aa, F_x1, F_x2, F_x3)

def getCharacters_arr(constel:numpy.ndarray,signal:numpy.ndarray,timeSeries,
                      carrierFreq:int,a_t:float=1.0,noiseRad:float=0.1*numpy.pi):
    """输入标注的所有参数，输出一维数组，长度为8，分别对应getCharacters中8个特征值，顺序不变"""
    CP=getCharacters(constel,signal,timeSeries,carrierFreq,a_t,noiseRad)
    return numpy.abs(numpy.array(CP)).copy()

def getCharaReco(constel:numpy.ndarray,signal:numpy.ndarray,timeSeries,
                 carrierFreq:int,a_t:float=1.0,noiseRad:float=0.1*numpy.pi):
    """输入多个constel、signal，输出所有序列指定特征值的平均值"""
    if type(constel)==numpy.ndarray and type(signal)==numpy.ndarray:
        if len(constel.shape)==1:
            constel=constel.reshape([1,-1])
        if len(signal.shape)==1:
            signal=signal.reshape([1,-1])
        f_x1=numpy.abs(f_x1_avg(constel))
        f_x2=numpy.abs(f_x2_avg(constel))
        f_x3=numpy.abs(f_x3_avg(constel))
        c20=numpy.abs(C20_avg(constel))
        c21=numpy.abs(C21_avg(constel))
        c63=numpy.abs(C63_avg(constel))
        sigma_af=numpy.abs(Sigma_af_avg(signal, timeSeries, carrierFreq, a_t, noiseRad))
        chara=charaReco(f_x1,f_x2,f_x3,c20,c21,c63,sigma_af)
        return chara
    else:
        raise ValueError("输入星座图和信号必须为ndarray类型")